package org.zjx.model.route;

import com.alibaba.fastjson2.annotation.JSONType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.zjx.entity.Location;
import org.zjx.enums.route.RouteType;
import org.zjx.model.game.Screen;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 飞行符传送路由类
 * 表示使用飞行符进行传送的路径
 */

@JSONType(typeName = "FlyingCharmRoute")
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class FlyingCharmRoute extends AbstractRoute {

    // 预设的飞行符目的地
// 预设的飞行符目的地
    public static final Map<String, Location> DESTINATIONS;

    static {
        Map<String, Location> tempMap = new HashMap<>();
        tempMap.put("长安城", new Location(getStaticScreen("长安城"), 361, 243));
        tempMap.put("傲来国", new Location(getStaticScreen("傲来国"), 123, 94));
        tempMap.put("长寿村", new Location(getStaticScreen("长寿村"), 111, 61));
        tempMap.put("朱紫国", new Location(getStaticScreen("朱紫国"), 140, 93));
        tempMap.put("西梁女国", new Location(getStaticScreen("西梁女国"), 101, 28));
        tempMap.put("宝象国", new Location(getStaticScreen("宝象国"), 115, 70));
        tempMap.put("建邺城", new Location(getStaticScreen("建邺城"), 65, 30));
        DESTINATIONS = Collections.unmodifiableMap(tempMap);
    }

    private String destinationCity;  // 目标城市名

    /**
     * 构造函数
     *
     * @param start    起点
     * @param cityName 城市名
     * @throws IllegalArgumentException 如果起点和终点在同一城市
     */
    public FlyingCharmRoute(Location start, String cityName) {
        super(start, getDestination(cityName), 15, RouteType.FLYING_CHARM, false);
        if (!DESTINATIONS.containsKey(cityName)) {
            throw new IllegalArgumentException("不支持的目的地: " + cityName);
        }
        this.destinationCity = cityName;
    }

    /**
     * 根据城市名获取目的地
     *
     * @param cityName 城市名
     * @return 目的地位置
     * @throws IllegalArgumentException 如果城市名不支持
     */
    private static Location getDestination(String cityName) {
        Location dest = DESTINATIONS.get(cityName);
        if (dest == null) {
            throw new IllegalArgumentException("Unsupported flying charm destination: " + cityName);
        }
        return dest;
    }

    /**
     * 创建反向路由(不支持)
     *
     * @throws UnsupportedOperationException 总是抛出
     */
    @Override
    protected Route createReverseRoute() {
        throw new UnsupportedOperationException("飞行符只能单向使用");
    }

    /**
     * 获取路由描述
     *
     * @return 描述字符串
     */
    @Override
    public String getDescription() {
        return String.format("使用飞行符从%s飞往%s，耗时%d秒", getStart().getScreen().getName(), destinationCity, getTime());
    }

    public static Screen getStaticScreen(String cityName) {
        Screen screen = new Screen();
        screen.setName(cityName);
        screen.setFatherId(0);
        screen.setOpenMap(true);
        screen.setFollow(true);
        switch (cityName) {
            case "长安城":
                screen.setId(1);
                screen.setWidth(548);
                screen.setHeight(278);
                break;
            case "傲来国":
                screen.setId(9);
                screen.setWidth(222);
                screen.setHeight(150);
                break;
            case "长寿村":
                screen.setId(36);
                screen.setWidth(159);
                screen.setHeight(208);
                break;
            case "朱紫国":
                screen.setId(29);
                screen.setWidth(191);
                screen.setHeight(119);
                break;
            case "西梁女国":
                screen.setId(35);
                screen.setWidth(163);
                screen.setHeight(123);
                break;
            case "宝象国":
                screen.setId(32);
                screen.setWidth(159);
                screen.setHeight(119);
                break;
            case "建邺城":
                screen.setId(3);
                screen.setWidth(287);
                screen.setHeight(143);
                break;
        }

        return screen;
    }
}